package arkham.data;

import arkham.jaxb.encounter.*;
import arkham.util.AHLogger;

import java.util.*;

/**
 * This class represents an environment and the effects it has when
 * active.
 * @author ctheng
 *
 */
public class Environment implements NamedEntity {
	private final static String OBJECT_TYPE = "Environment";
	private String name;
	private String type;
	private String description;
	private String target;
	private boolean fromMythos;
	private int duration;
	private int remainingDuration; 
	
	private List<EffectType> environmentEffects;
	
	public Environment(String name)
	{
		this.name = name;
		environmentEffects = new ArrayList<EffectType>();
		target = "";
		description = "";
		duration = 0;
	}
	
	/**
	 * Initializes this environment
	 */
	public void initialize()
	{
		remainingDuration = duration;
	}
	
	/**
	 * Gets the name of the environment
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * Gets the type of the environment
	 * @return the type
	 */
	public String getType() {
		return type;
	}

	/**
	 * Gets the target of the environment
	 * @return the target
	 */
	public String getTarget() {
		return target;
	}

	/**
	 * Gets the duration of the environment
	 * @return the duration
	 */
	public int getDuration() {
		return duration;
	}
	
	/**
	 * @return the description
	 */
	public String getDescription()
	{
		return description;
	}

	/**
	 * Gets the remaining time of this environment
	 * @return the remaining time
	 */
	public int getRemainingDuration()
	{
		return remainingDuration;
	}
	
	/**
	 * Decrements the remaining time of this environment by 1
	 */
	public void decrementDuration()
	{
		remainingDuration--;
	}
	
	/**
	 * Gets a list of effects caused by the environment
	 * @return the environmentEffects
	 */
	public List<EffectType> getEnvironmentEffects() {
		return environmentEffects;
	}
	
	/**
	 * @param fromMythosEnvironment true if this effect is from a mythos 
	 * environment
	 */
	public void setFromMythos(boolean fromMythos)
	{
		this.fromMythos = fromMythos;
	}

	/**
	 * Checks if this effect is caused by a mythos environment, and hence
	 * should be removed when a new environment appears
	 * @return true if this effect is from a mythos environment
	 */
	public boolean isFromMythos()
	{
		return fromMythos;
	}

	@Override
	public String toString()
	{
		return name;
	}
	
	/**
	 * Creates the data representation of the environment from the XML info
	 * and performs a stringent validation check to make sure the XML data is
	 * valid. 
	 * <br>Will return <b>null</b> on a failure.
	 * 
	 * @param xmlenv the XML environment info
	 * @return the data representation of the environment
	 */
	public static Environment createEnvironmentFromXml(EnvironmentType xmlenv)
	{
		Environment env = new Environment(xmlenv.getName());
		env.type = xmlenv.getType();
		if (!Globals.validateType(OBJECT_TYPE, env.type))
		{
			AHLogger.error("Environment " + env.name + " has invalid type: " + env.type);
			return null;
		}
		env.target = xmlenv.getWho();
		if (xmlenv.getDuration() != null)
		{
			env.duration = xmlenv.getDuration();
		}
		env.fromMythos = xmlenv.isFromMythos();
		if (xmlenv.getDescription() != null)
		{
			env.description = xmlenv.getDescription();	
		}
		env.environmentEffects.addAll(xmlenv.getEffect());
		
		return env;
	}
}
